package com.lu.service.user;

import com.lu.entity.User;
import com.lu.entity.UsernameAndPassword;
import com.lu.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@Service
public class UserService implements UserServices{
    @Autowired
    private UserMapper userMapper;

    //查询数据库中账号密码
    @Override
    public List<UsernameAndPassword> pwdAndUser() {
        List<User> alldata = userMapper.query();
        List<UsernameAndPassword> newdata = new ArrayList<>();
        for (int i = 0; i < alldata.size(); i++) {
            User user = alldata.get(i);
            String username = user.getUsername();
            String password = user.getPassword();
            UsernameAndPassword usernameAndPassword = new UsernameAndPassword();
            usernameAndPassword.setUsername(username);
            usernameAndPassword.setPassword(password);
            newdata.add(usernameAndPassword);
        }
        return newdata;
    }

    //查询数据库中账号密码和邮箱
    @Override
    public List<User> all() {
        return userMapper.query();
    }

    //插入数据
    @Override
    public void insert(User user) {
        user.setCreatedTime(LocalDateTime.now());
        userMapper.insert(user);
    }

    //返回用户名数据
    @Override
    public String[] username() {
        return userMapper.username();
    }

    //获取邮箱数据
    @Override
    public String[] email() {
        List<User> users = userMapper.query();
        String[] emails = new String[users.size()];
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            emails[i] = user.getEmail();
        }
        return emails;
    }

    //修改密码
    @Override
    public void updatePassword(User user) {
        userMapper.updatePassword(user);
    }

}
